package com.dhc.service.controller;

import com.dhc.service.utils.SendEmailCode;
import com.dhc.service.utils.result.Result;
import lombok.NonNull;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.Duration;
import java.util.Random;

/**
 * @Author kazu
 * @Date 2023/10/07 23:20
 * @Version 1.0
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("sys/email")
@CrossOrigin
public class EmailController {
	
	@Autowired
	private SendEmailCode EmailCode;

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	@GetMapping("getEmailCode")
	public Result getEmailCode(@Param(value = "email") @NonNull String email) {
		String emailCode = String.valueOf(new Random().nextInt(899999) + 100000);
		String info = "您的注册验证码为： " + emailCode;
		String notice = " 请不要把验证码泄露给其他人，如非本人操作，请忽略该短信！";
		EmailCode.sendEmailCode(email, "学生管理系统", info + notice);
		System.out.println(emailCode);
		redisTemplate.opsForValue().set(email, emailCode, Duration.ofMinutes(3));
		return Result.ok().message("验证码已发送");
	}
}
